<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-5-11
 * Time: 9:11
 */

namespace app\manage\admin;
use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\common\model\Icon as IconModel;
use pinyin\Pinyin;
use think\Db;

class Icon extends Admin
{

    //公告列表
    public function lists(){
        // 读取数据
        $map = $this->getMap();
        // 排序
        $order = $this->getOrder();
        // 数据列表
        $data_list = IconModel::where($map)->order($order)->paginate();
        // 分页数据
        $page = $data_list->render();
        $btn_access = [
            'title' => '新增图标',
            'icon' => 'fa fa-fw fa-plus',
            'class' => 'btn btn-info',
            'href' => url('add'),
        ];
        $btn_access1 = [
            'title' => '编辑图标',
            'icon' => 'fa fa-fw fa-pencil',
            'class' => 'btn btn-xs btn-default',
            'href' => url('edit', ['id' => '__id__']),
        ];
        $btn_access2 = [
            'title' => '删除图标',
            'icon' => 'fa fa-fw fa-trash-o',
            'class' => 'btn btn-xs btn-default ajax-get confirm',
            'href' => url('delete', ['id' => '__id__']),
        ];
        //设置页面
        return ZBuilder::make('table')
            ->hideCheckbox()
            ->addOrder('id')
            ->addColumn('id', '编号', 'text')
            ->addColumn('name', '图标名称', 'text')
            ->addColumn('image', '图标', 'picture')
            ->addColumn('type', '类型', 'status','',['1'=>'本地链接','2'=>'外部链接'])
            ->addColumn('href', '链接地址', 'text')
            ->addColumn('sort', '排序', 'text')
            ->addColumn('created_time', '发布日期', 'datetime', '', 'Y-m-d H:i:s')
            ->addColumn('right_button', '操作', 'btn')
            ->setTableName('banner_position')// 指定数据表名
            ->addTopButton('custom', $btn_access)// 添加顶部按钮
            ->addRightButton('delete', $btn_access1)// 添加右侧按钮
            ->addRightButton('banner', $btn_access2)// 添加右侧按钮
            ->setPrimaryKey('id')// 设置主键名为username
            ->setColumnWidth([
                'id'  => 30,
                'name' => 50,
                'image'=>40,
                'type'=>40,
                'href'=>350,
                'sort'=>50,
                'created_time'=>100,
                'right_button'=>80
            ])
            ->setPageTitle('图标列表')
            ->setRowList($data_list)// 设置表格数据
            ->setPages($page)// 设置分页数据
            ->fetch();
    }

    //添加品牌
    public function add()
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            $result = $this->validate($data, 'Icon.add');
            if(true !== $result) $this->error($result);
            $data['created_time'] = time();
            if (IconModel::create($data)) {
                $this->success('新增成功', 'lists');
            } else {
                $this->error('新增失败');
            }
        } else {
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['text:7', 'name', '图标名称'],
                        ['image', 'image', '图标'],
                        ['text', 'sort', '排序','<code>排序0-100，越小越靠前</code>','50'],
                        ['radio', 'type', '跳转类型', '', [1 => '本地链接', 2 => '外部链接'],'1'],
                        ['text', 'href', '链接地址', ''],
                        ['radio', 'status', '状态', '', [1 => '显示', 2 => '隐藏'], '1'],
                    ]
                )
                ->setPageTitle('添加图标')
                ->fetch();
        }
    }


    //编辑公告
    public function edit($id = null)
    {
        //判断是否提交信息
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Icon.add');
            if(true !== $result) $this->error($result);
            $data['created_time'] = time();
            if (IconModel::update($data)) {
                $this->success('编辑成功', 'lists');
            } else {
                $this->error('编辑失败');
            }
        } else {
            $info = IconModel::get($id);
            return ZBuilder::make('form')
                ->addFormItems(
                    [
                        ['hidden', 'id', 'id'],
                        ['text:7', 'name', '图标名称'],
                        ['image', 'image', '图片'],
                        ['text', 'sort', '排序','<code>排序0-100，越小越靠前</code>'],
                        ['radio', 'type', '跳转类型', '', [1 => '本地链接', 2 => '外部链接']],
                        ['text', 'href', '链接地址', ''],
                        ['radio', 'status', '状态', '', [1 => '显示', 2 => '隐藏']],
                    ]
                )
                ->setFormData($info)
                ->setPageTitle('编辑图标')
                ->fetch();
        }
    }

    //删除公告
    public function delete($id = null)
    {
        if ($id === null) $this->error('缺少参数');
        $re = IconModel::destroy($id);//
        if ($re) {
            $this->success('删除成功', 'lists');
        } else {
            $this->error('系统繁忙请稍后再试', 'lists');
        }

    }
}